package com.kanshu.ksgb.fastread.doudou.common.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;

/**
 * 项目名称：client
 * 类描述：标题栏
 * 创建人：wanchunyang
 * 创建时间：2017/4/27 17:10
 * 修改人：wanchunyang
 * 修改时间：2017/4/27 17:10
 * 修改备注：
 */
public class TitlebarView extends LinearLayout implements View.OnClickListener{

    private TextView mLeftText;
    private TextView mMiddText;
    private TextView mMiddSubText;
    private FrameLayout mRight;
    private FrameLayout mLeft;
    private LinearLayout mMiddle;
    private FrameLayout mRoot;
    public TitlebarView(Context context) {
        super(context);
        init();
    }

    public TitlebarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TitlebarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.titlebar_layout, this, true);
        findViewById(R.id.left).setOnClickListener(this);
        mLeftText = (TextView) findViewById(R.id.back_title);
        mMiddText = (TextView) findViewById(R.id.main_title);
        mMiddSubText = (TextView) findViewById(R.id.sub_title);
        mRight = (FrameLayout) findViewById(R.id.right);
        mLeft = (FrameLayout) findViewById(R.id.left);
        mMiddle = (LinearLayout) findViewById(R.id.middle);
        mRoot = (FrameLayout) findViewById(R.id.title_root);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left:
                back();
                break;
            default:
                break;
        }
    }

    private void back() {
        ((Activity) getContext()).finish();
    }

    public void setTitle(CharSequence s) {
        if (!TextUtils.isEmpty(s)) {
            mMiddText.setText(s);
        }
    }

    public void goneLeftContainer() {
        DisplayUtils.gone(mLeft);
    }

    public void goneRightContainer() {
        DisplayUtils.gone(mRight);
    }

    public void setSubTitle(CharSequence s) {
        mMiddSubText.setText(s);
    }

    public void setLeftText(CharSequence s) {
        mLeftText.setText(s);
    }

    public FrameLayout getRightContainer() {
        return mRight;
    }

    public FrameLayout getLeftContainer() {
        return mLeft;
    }

    public LinearLayout getMiddleContainer() {
        return mMiddle;
    }
    public FrameLayout getRootContainer() {
        return mRoot;
    }

    public TextView getBack() {
        return mLeftText;
    }

    public void setBgColor(int color) {
        setBackgroundColor(color);
    }

}
